<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>来访管理</title>
    <link rel="stylesheet" href="css/bootstrap.css">
    <link rel="stylesheet" href="css/main.css">
    <script src="js/jquery-3.5.1.js"></script>
    <script src="js/bootstrap.js"></script>
</head>

<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">访客管理系统</span> <span class="icon-bar"></span>
                <span class="icon-bar"></span> <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">访客管理系统</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">欢迎：<span style="color: yellow">${loginUser.username}</span></a></li>
                <li><a href="authLogout">退出</a></li>
            </ul>
        </div>
    </div>
</nav>

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
            <!-- 侧边栏 -->
            <jsp:include page="menu.jsp">
                <jsp:param value="active" name="Laifang_active"/>
            </jsp:include>
        </div>
        <br>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="row">
                <div class="col-sm-7">
                    <div class="input-group">
                        <input class="form-control" type="hidden" id="searchColumn" name="searchColumn" value="laifang_name"/>
                        <input class="form-control" type="text" id="search_keyword" name="search_keyword" placeholder="姓名"/> <span class="input-group-btn"><button class="btn btn-line btn-default" type="button" onclick="searchList()">搜索</button></span>
                    </div>
                </div>
                <div class="col-sm-5">
                    <button type="button" <c:if test="${loginUser.userType != '管理员'}">disabled="disabled" title="没有权限！！！"</c:if> class="btn btn-line btn-warning" data-toggle="modal" data-target="#modal-add">添加来访
                    </button>
                </div>
            </div>
            <br>
            <br>
            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead>
                    <tr>
                            <th>姓名</th>
                            <th>性别</th>
                            <th>身份证</th>
                            <th>电话</th>
                            <th>来访时间</th>
                            <th>离开时间</th>
                            <th>事由</th>
                            <th>备注</th>
                        <th style="text-align: center;">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <c:forEach items="${list}" var="vo">
                        <tr>
                <td>${vo.laifangName}</td>
                <td>${vo.laifangSex}</td>
                <td>${vo.laifangIdno}</td>
                <td>${vo.laifangPhone}</td>
                <td>${vo.laifangIndate}</td>
                <td>${vo.laifangOutdate}</td>
                <td>${vo.laifangReason}</td>
                <td title="${vo.laifangText}">
                <c:choose>
                    <c:when test="${fn:length(vo.laifangText) > 19}">
                        <c:out value="${fn:substring(vo.laifangText, 0, 19)}..."/>
                    </c:when>
                    <c:otherwise>
                        <c:out value="${vo.laifangText}"/>
                    </c:otherwise>
                </c:choose>
                </td>
                            <th style="text-align: center;">
                                <button class="btn btn-line btn-primary btn-sm" data-id="${vo.id}"
                                        data-toggle="modal" data-target="#modal-info">详情
                                </button>
                                <button class="btn btn-line btn-default btn-sm"
                                                <c:if test="${loginUser.userType != '管理员'}">disabled="disabled" title="没有权限！！！"</c:if>
                                        data-id="${vo.id}"
                                        data-toggle="modal" data-target="#modal-edit">编辑
                                </button>
                                <button class="btn btn-line btn-danger btn-sm" <c:if test="${loginUser.userType != '管理员'}">disabled="disabled" title="没有权限！！！"</c:if> data-id="${vo.id}"
                                        data-toggle="modal" data-target="#modal-delete">删除
                                </button>
                            </th>
                        </tr>
                    </c:forEach>
                    </tbody>
                </table>
            </div>
            <div style="float: right;padding-right: 10px;color: #515151;"><jsp:include page="split.jsp"/></div>
        </div>
    </div>
</div>

<!-- add -->
<div class="modal fade" id="modal-add" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <form action="laifangAdd" onsubmit="return addCheck()">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"
                            aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel">增加来访</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group hidden">
                        <label class="control-label">(hidden)</label>
                        <input type="text" class="form-control" name="action" value="add">
                    </div>
                    <div class="form-group">
                        <label for="add-laifangName" class="control-label">姓名:</label>
                        <input type="text" class="form-control" name="laifangName" id="add-laifangName">
                    </div>
                    <div class="form-group">
                        <label class="control-label">性别:</label>
                        <input name="laifangSex" id="add-laifangSex_男" type="radio" value="男" checked="checked"/>男
                        <input name="laifangSex" id="add-laifangSex_女" type="radio" value="女"/>女
                    </div>
                    <div class="form-group">
                        <label for="add-laifangIdno" class="control-label">身份证:</label>
                        <input type="text" class="form-control" name="laifangIdno" id="add-laifangIdno">
                    </div>
                    <div class="form-group">
                        <label for="add-laifangPhone" class="control-label">电话:</label>
                        <input type="text" class="form-control" name="laifangPhone" id="add-laifangPhone">
                    </div>
                    <div class="form-group">
                        <label for="add-laifangIndate" class="control-label">来访时间:</label>
                        <input type="text" class="form-control" name="laifangIndate" id="add-laifangIndate">
                    </div>
                    <div class="form-group">
                        <label for="add-laifangOutdate" class="control-label">离开时间:</label>
                        <input type="text" class="form-control" name="laifangOutdate" id="add-laifangOutdate">
                    </div>
                    <div class="form-group">
                        <label for="add-laifangReason" class="control-label">事由:</label>
                        <input type="text" class="form-control" name="laifangReason" id="add-laifangReason">
                    </div>
                    <div class="form-group">
                        <label for="add-laifangText" class="control-label">备注:</label>
                        <textarea style="height: 100px;" class="form-control" name="laifangText" id="add-laifangText"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-line btn-warning" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-line btn-warning">提交</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- info -->
<div class="modal fade" id="modal-info" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <form>
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title">来访</h4>
                </div>
                <div class="modal-body">
                    <table class="table table-striped table-hover" style="font-size: 15px;">
                        <tr>
                            <td style="width: 15%;">姓名:</td>
                            <td><b id="info-laifangName"></b></td>
                        </tr>
                        <tr>
                            <td style="width: 15%;">性别:</td>
                            <td><b id="info-laifangSex"></b></td>
                        </tr>
                        <tr>
                            <td style="width: 15%;">身份证:</td>
                            <td><b id="info-laifangIdno"></b></td>
                        </tr>
                        <tr>
                            <td style="width: 15%;">电话:</td>
                            <td><b id="info-laifangPhone"></b></td>
                        </tr>
                        <tr>
                            <td style="width: 15%;">来访时间:</td>
                            <td><b id="info-laifangIndate"></b></td>
                        </tr>
                        <tr>
                            <td style="width: 15%;">离开时间:</td>
                            <td><b id="info-laifangOutdate"></b></td>
                        </tr>
                        <tr>
                            <td style="width: 15%;">事由:</td>
                            <td><b id="info-laifangReason"></b></td>
                        </tr>
                        <tr>
                            <td style="width: 15%;">备注:</td>
                            <td><b id="info-laifangText"></b></td>
                        </tr>
                    </table>
                    <br>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-line btn-warning" data-dismiss="modal">关闭</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- edit -->
<div class="modal fade" id="modal-edit" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <form action="laifangEdit" onsubmit="return editCheck()">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title">更新来访</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group hidden">
                        <label class="control-label">(hidden)</label>
                        <input type="text" class="form-control" name="action" value="edit">
                        <input type="text" readonly class="form-control" name="id" id="edit-id">
                    </div>
                    <div class="form-group">
                        <label for="edit-laifangName" class="control-label">姓名:</label>
                        <input type="text" class="form-control" name="laifangName" id="edit-laifangName">
                    </div>
                    <div class="form-group">
                        <label class="control-label">性别:</label>
                               <input name="laifangSex" id="edit-laifangSex_男" type="radio" value="男"/>男
                               <input name="laifangSex" id="edit-laifangSex_女" type="radio" value="女"/>女
                    </div>
                    <div class="form-group">
                        <label for="edit-laifangIdno" class="control-label">身份证:</label>
                        <input type="text" class="form-control" name="laifangIdno" id="edit-laifangIdno">
                    </div>
                    <div class="form-group">
                        <label for="edit-laifangPhone" class="control-label">电话:</label>
                        <input type="text" class="form-control" name="laifangPhone" id="edit-laifangPhone">
                    </div>
                    <div class="form-group">
                        <label for="edit-laifangIndate" class="control-label">来访时间:</label>
                        <input type="text" class="form-control" name="laifangIndate" id="edit-laifangIndate">
                    </div>
                    <div class="form-group">
                        <label for="edit-laifangOutdate" class="control-label">离开时间:</label>
                        <input type="text" class="form-control" name="laifangOutdate" id="edit-laifangOutdate">
                    </div>
                    <div class="form-group">
                        <label for="edit-laifangReason" class="control-label">事由:</label>
                        <input type="text" class="form-control" name="laifangReason" id="edit-laifangReason">
                    </div>
                    <div class="form-group">
                        <label for="edit-laifangText" class="control-label">备注:</label>
                        <textarea style="height: 100px;" class="form-control" name="laifangText" id="edit-laifangText"></textarea>
                    </div>
                    <div class="form-group hidden">
                        <label for="edit-createTime" class="control-label">创建时间:</label>
                        <input type="text" class="form-control" name="createTime" id="edit-createTime">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-line btn-warning" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-line btn-warning">提交</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- delete -->
<div class="modal fade" id="modal-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <form action="laifangDelete">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title">删除来访</h4>
                </div>
                <div class="modal-body">
                    确认要删除该来访记录吗？
                    <div class="form-group hidden">
                        <label class="control-label">(hidden)</label>
                        <input type="hidden" class="form-control" name="action" value="delete">
                        <input type="text" class="form-control" name="id" id="delete-id">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-line btn-warning" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-line btn-danger">删除</button>
                </div>
            </form>
        </div>
    </div>
</div>

</body>
<script>
    $('#modal-delete').on('show.bs.modal', function (event) {
        let button = $(event.relatedTarget);
        let id = button.data('id');
        let modal = $(this);
        modal.find('#delete-id').val(id);
    })

    $('#modal-edit').on('show.bs.modal', function (event) {
        let button = $(event.relatedTarget);
        let id = button.data('id');
        let modal = $(this);
        $.ajax({
            url: 'laifangGet?id=' + id,
            type: "get",
            success: function (voString) {
                let vo = eval('(' + voString + ')');
                        modal.find('#edit-id').val(vo.id);
                        modal.find('#edit-laifangName').val(vo.laifangName);
                        for (let val of "男/女".split('/')) {
                            if (val == vo.laifangSex) {
                                modal.find('#edit-laifangSex_' + vo.laifangSex).prop("checked", true);
                            } else {
                                modal.find('#edit-laifangSex_' + vo.laifangSex).removeAttr("checked");
                            }
                        };
                        modal.find('#edit-laifangIdno').val(vo.laifangIdno);
                        modal.find('#edit-laifangPhone').val(vo.laifangPhone);
                        modal.find('#edit-laifangIndate').val(vo.laifangIndate);
                        modal.find('#edit-laifangOutdate').val(vo.laifangOutdate);
                        modal.find('#edit-laifangReason').val(vo.laifangReason);
                        modal.find('#edit-laifangText').val(vo.laifangText);
            }
        })
    })
    $('#modal-info').on('show.bs.modal', function (event) {
        let button = $(event.relatedTarget);
        let id = button.data('id');
        let modal = $(this);
        $.ajax({
            url: 'laifangGet?id=' + id,
            type: "get",
            success: function (voString) {
                let vo = eval('(' + voString + ')');
                modal.find('#info-laifangName').text(vo.laifangName);
                modal.find('#info-laifangSex').text(vo.laifangSex);
                modal.find('#info-laifangIdno').text(vo.laifangIdno);
                modal.find('#info-laifangPhone').text(vo.laifangPhone);
                modal.find('#info-laifangIndate').text(vo.laifangIndate);
                modal.find('#info-laifangOutdate').text(vo.laifangOutdate);
                modal.find('#info-laifangReason').text(vo.laifangReason);
                modal.find('#info-laifangText').text(vo.laifangText);
            }
        })
    })
    function searchList() {
        window.location.href = "laifangList?searchColumn="+document.getElementById("searchColumn").value+"&keyword=" + document.getElementById("search_keyword").value;
    }
    //增加表单提交之前进行检查，如果return false，则不允许提交
    function addCheck() {
        //根据ID获取值
        if (document.getElementById("add-laifangName").value.trim().length == 0) {
            alert("姓名不能为空");
            return false;
        }
        if (document.getElementById("add-laifangIdno").value.trim().length == 0) {
            alert("身份证不能为空");
            return false;
        }
        if (document.getElementById("add-laifangPhone").value.trim().length == 0) {
            alert("电话不能为空");
            return false;
        }
        if (document.getElementById("add-laifangIndate").value.trim().length == 0) {
            alert("来访时间不能为空");
            return false;
        }
        if (document.getElementById("add-laifangOutdate").value.trim().length == 0) {
            alert("离开时间不能为空");
            return false;
        }
        if (document.getElementById("add-laifangReason").value.trim().length == 0) {
            alert("事由不能为空");
            return false;
        }
        return true;
    }
    //编辑表单提交之前进行检查，如果return false，则不允许提交
    function editCheck() {
        //根据ID获取值
        if (document.getElementById("edit-laifangName").value.trim().length == 0) {
            alert("姓名不能为空");
            return false;
        }
        if (document.getElementById("edit-laifangIdno").value.trim().length == 0) {
            alert("身份证不能为空");
            return false;
        }
        if (document.getElementById("edit-laifangPhone").value.trim().length == 0) {
            alert("电话不能为空");
            return false;
        }
        if (document.getElementById("edit-laifangIndate").value.trim().length == 0) {
            alert("来访时间不能为空");
            return false;
        }
        if (document.getElementById("edit-laifangOutdate").value.trim().length == 0) {
            alert("离开时间不能为空");
            return false;
        }
        if (document.getElementById("edit-laifangReason").value.trim().length == 0) {
            alert("事由不能为空");
            return false;
        }
        return true;
    }
</script>
</html>
